layui.use(['jquery','layer','table'],function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var table = layui.table;

    var account="";
    var nickname="";
    var endDate = new Date();
    endDate = getTime(endDate);
    select('/Coupons/selectUserCoupons?account='+account+"&nickname="+nickname+"&endDate="+endDate);
    function select(url) {
        table.render({
            elem:"#root",
            url:url,
            toolbar:"#lefttool",
            defaultToolbar: ['','',''],
            page:true,
            limit:5,
            limits:[5,10,15,20],
            size: 'lg',
            cols:[[
                {type:"checkbox",align:"center"},
                {type:"numbers",title:"序号",align:"center"},
                {field:"account",title:"用户账号",align:"center"},
                {field:"nickname",title:"用户昵称",align:"center"},
                {field:"used",title:"已使用",align:"center"},
                {field:"noUsed",title:"未使用",align:"center"},
                {field:"countEnd",title:"已过期",align:"center"},
                {title:"操作",align:"center",templet:function (data) {
                        return "<div>"+"<a href='javascript:giveCoupons("+data.userId+");' style='color: #0EAA9E;margin-right: 10px'>赠送优惠券</a>"+"</div>"
                    }}
            ]]
        })
    };
    //搜索
    $("#sousuo").click(function () {
        var account = $("#account").val();
        var nickname = $("#nickname").val();
        select('/Coupons/selectUserCoupons?account='+account+"&nickname="+nickname+"&endDate="+endDate);
    });
    //重置
    $("#chongzhi").click(function () {
        select('/Coupons/selectUserCoupons?account='+account+"&nickname="+nickname+"&endDate="+endDate);
    });
    giveCoupons = function giveCoupons(userId) {
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            title:'赠送优惠券',
            content:'/coupon/coupon-give.html?userId='+userId,
            area:['900px', '620px'],
            success:function(layero,index){
                obj = layero.find('iframe').contents();
                obj.find("input[name=userId]").val(userId);
            }
        })
    };
    //顶部时间
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };

    //时间的为零操作
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    };

    $("#shuaxin").click(function () {
        location.reload();
    });
    //跳转
    goBack=function goBack(){
            //通过页面查找iframe
            parent.$('#recordLog').val($('#examine').text());
            parent.before();
            return false;
        };
});
